import fs from 'fs'
import { NodeSSH } from 'node-ssh'

const ssh = new NodeSSH()

async function deploy() {
  try {
    console.log('开始连接服务器...')

    await ssh.connect({
      host: 'micefind.com',
      port: 22,
      username: 'root',
      privateKey: fs.readFileSync('C:/users/micefind/.ssh/micefind/id_rsa.pem', 'utf-8'),
    })

    console.log('连接成功，开始上传...')

    const localPath = './dist'
    const remotePath = '/opt/html/blog/'

    const success = await ssh.putDirectory(localPath, remotePath, {
      recursive: true,
      concurrency: 5, // 并发传输线程数
      validate: () => true, // 所有文件都上传
    })

    if (success) {
      console.log('上传成功')
    } else {
      console.log('部分文件上传失败')
    }

    ssh.dispose()
  } catch (err) {
    console.error('上传失败:', err)
  }
}

deploy()
